package com.jsz.peini.service.label;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jsz.peini.bean.label.LabelCondition;
import com.jsz.peini.bean.label.LabelInfo;
import com.jsz.peini.bean.label.LabelUser;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.mapper.label.LabelInfoMapper;
import com.jsz.peini.mapper.label.LabelUserMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;

@Service
public class LabelInfoService {
	
	@Resource
	private LabelInfoMapper labelInfoMapper;
	
	@Resource
	private LabelUserMapper labelUserMapper;
	
	@Resource
	private UserInfoMapper userInfoMapper;
	
	public List<LinkedHashMap<String,Object>> selectLabelUserByUserInfoId(String UserInfoId){
		List<LinkedHashMap<String,Object>> selectLabelUserByUserInfoId=labelUserMapper.selectLabelUserByUserInfoId(UserInfoId);
		return selectLabelUserByUserInfoId;
	}
	
	//获取所有符合条件标签并更新
	public List<LabelUser> goLabel(String userId){
		//获取当前用户的所有信息
		HashMap<String,Object> userMap = userInfoMapper.selectMapByUserId(userId);
		labelUserMapper.deleteLabelUserByUserId(userId);
		//获取所有标签
		List<LabelInfo> labelInfoList = labelUserMapper.selectAllInfo();
		
		List<LabelUser> labelUser = new ArrayList<LabelUser>();//自定义 
		
		for(LabelInfo labelInfo : labelInfoList){
			boolean flag = true;
			int result = 0;//设置初始化条件
			//获取所有标签所有条件
			List<LabelCondition> labelConditionList = labelUserMapper.selectAllCondition(labelInfo.getId());
			if (labelConditionList.get(0).getLabelField().equals("定制标签")) {
				flag = false;
				break;
			}
			for(LabelCondition labelCondition : labelConditionList){
				if(userMap.get(labelCondition.getLabelField())==null){
					flag = false;
					continue;
				}
				switch (labelCondition.getLabelField()) {
				case "nickname":
					if(!labelCondition.getConditionOne().equals(userMap.get("nickname"))){
						flag = false;
					}if (userMap.get("nickname").equals("0")) {
						flag = false;
					}
					break;
				case "sign_word":
					if(!labelCondition.getConditionOne().equals(userMap.get("sign_word"))){
						flag = false;
					}if (userMap.get("sign_word").equals("0")) {
						flag = false;
					}
					break;
				case "image_head":
					if(userMap.get("image_head").equals("0") || StringUtils.isBlank(userMap.get("image_head").toString()) ){
						flag = false;
					}
					break;
				case "sex":
					if(!labelCondition.getConditionOne().equals(userMap.get("sex")+"")){
						flag = false;
					}if (userMap.get("sex").equals("0")) {
						flag = false;
					}
					break;
				case "birthday":
					Date start = DateTimeUtils.formatString2Date(labelCondition.getConditionOne(), "yyyy-MM-dd");
					Date end = DateTimeUtils.formatString2Date(labelCondition.getConditionTwo(), "yyyy-MM-dd");
					if(userMap.get("birthday") .equals("0")){
						flag = false;
					}else{
						Date birthday = DateTimeUtils.formatString2Date(userMap.get("birthday").toString(), "yyyy-MM-dd");
						if(birthday.after(start) && birthday.before(end)){
							
						}else{
							flag = false;
						}
					}
					break;
				case "age":
					if(userMap.get("age") .equals("0")){
						flag = false;
					}
					Integer low = Integer.parseInt(labelCondition.getConditionOne().toString());
					Integer hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer age = Integer.parseInt(userMap.get("age").toString());
					if(age >= low && age <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "height":
					if(userMap.get("height") .equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer height = Integer.parseInt(userMap.get("height").toString());
					if(height >= low && height <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "weight":
					if(userMap.get("weight") .equals("0")){
						flag = false;
					}
					low = Integer.parseInt(StringUtils.isBlank(labelCondition.getConditionOne()) ? "0" : labelCondition.getConditionOne());
					hig = Integer.parseInt(StringUtils.isBlank(labelCondition.getConditionTwo()) ? "0" : labelCondition.getConditionTwo());
					Integer weight = Integer.parseInt(userMap.get("weight").toString());
					if(weight >= low && weight <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "old_province":
					if(!labelCondition.getConditionOne().equals(userMap.get("old_province"))){
						flag = false;
					}if (userMap.get("old_province").equals("0")) {
						flag = false;
					}
					break;
				case "old_city":
					if(!labelCondition.getConditionOne().equals(userMap.get("old_city")+"")){
						flag = false;
					}if (userMap.get("old_city").equals("0")) {
						flag = false;
					}
					break;
				case "old_county":
					if(!labelCondition.getConditionOne().equals(userMap.get("old_county")+"")){
						flag = false;
					}if (userMap.get("old_county").equals("0")) {
						flag = false;
					}
					break;
				case "now_province":
					if(!labelCondition.getConditionOne().equals(userMap.get("now_province")+"")){
						flag = false;
					}if (userMap.get("now_province").equals("0")) {
						flag = false;
					}
					break;
				case "now_city":
					if(!labelCondition.getConditionOne().equals(userMap.get("now_city")+"")){
						flag = false;
					}if (userMap.get("now_city").equals("0")) {
						flag = false;
					}
					break;
				case "now_county":
					if(!labelCondition.getConditionOne().equals(userMap.get("now_county")+"")){
						flag = false;
					}if (userMap.get("now_county").equals("0")) {
						flag = false;
					}
					break;
				case "constellation":
					if(!labelCondition.getConditionOne().equals(userMap.get("constellation")+"")){
						flag = false;
					}if (userMap.get("constellation").equals("0")) {
						flag = false;
					}
					break;
				case "big_income":
					if(userMap.get("big_income") .equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					if(labelCondition.getConditionTwo() != "0"){
						hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
						Integer big_income = Integer.parseInt(userMap.get("big_income").toString());
						if(big_income >= low && big_income <= hig){
							
						}else{
							flag = false;
						}
					}else{
						Integer big_income = Integer.parseInt(userMap.get("big_income").toString());
						if(big_income >= low){
							
						}else{
							flag = false;
						}
					}
					break;
				case "degree":
					if(!labelCondition.getConditionOne().equals(userMap.get("degree")+"")){
						flag = false;
					}if (userMap.get("nickname").equals("0")) {
						flag = false;
					}
					break;
				case "industry":
					if(!labelCondition.getConditionOne().equals(userMap.get("industry")+"")){
						flag = false;
					}if (userMap.get("industry").equals("0")) {
						flag = false;
					}
					break;
				case "is_house":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_house")+"")){
						flag = false;
					}if (userMap.get("is_house").equals("0")) {
						flag = false;
					}
					break;
				case "is_car":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_car")+"")){
						flag = false;
					}if (userMap.get("is_car").equals("0")) {
						flag = false;
					}
					break;
				case "is_phone":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_phone")+"")){
						flag = false;
					}if (userMap.get("is_phone").equals("0")) {
						flag = false;
					}
					break;
				case "is_idcard":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_idcard")+"")){
						flag = false;
					}if (userMap.get("is_idcard").equals("0")) {
						flag = false;
					}
					break;
				case "is_video":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_video")+"")){
						flag = false;
					}if (userMap.get("is_video").equals("0")) {
						flag = false;
					}
					break;
				case "emotion":
					if(!labelCondition.getConditionOne().equals(userMap.get("emotion")+"")){
						flag = false;
					}if (userMap.get("emotion").equals("0")) {
						flag = false;
					}
					break;
				case "reputation":
					if(userMap.get("reputation").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer reputation = Integer.parseInt(userMap.get("reputation").toString());
					if(reputation >= low && reputation <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "reputation_year":
					if(userMap.get("reputation_year").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer reputation_year = Integer.parseInt(userMap.get("reputation_year").toString());
					if(reputation_year >= low && reputation_year <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "reputation_quarter":
					if(userMap.get("reputation_quarter").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer reputation_quarter = Integer.parseInt(userMap.get("reputation_quarter").toString());
					if(reputation_quarter >= low && reputation_quarter <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "reputation_month":
					if(userMap.get("reputation_month").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer reputation_month = Integer.parseInt(userMap.get("reputation_month").toString());
					if(reputation_month >= low && reputation_month <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "gold_list":
					if(userMap.get("gold_list").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer gold_list = Integer.parseInt(userMap.get("gold_list").toString());
					if(gold_list >= low && gold_list <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "buy_list":
					if(userMap.get("buy_list").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer buy_list = Integer.parseInt(userMap.get("buy_list").toString());
					if(buy_list >= low && buy_list <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "integrity_list":
					if(userMap.get("integrity_list").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer integrity_list = Integer.parseInt(userMap.get("integrity_list").toString());
					if(integrity_list >= low && integrity_list <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "user_life":
					if(userMap.get("user_life").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer user_life = Integer.parseInt(userMap.get("user_life").toString());
					if(user_life >= low && user_life <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "user_image":
					if(userMap.get("user_image").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer user_image = Integer.parseInt(userMap.get("user_image").toString());
					if(user_image >= low && user_image <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "user_date":
					start = DateTimeUtils.formatString2Date(labelCondition.getConditionOne(), "yyyy-MM-dd");
					end = DateTimeUtils.formatString2Date(labelCondition.getConditionTwo(), "yyyy-MM-dd");
					if(userMap.get("user_date").equals("0")){
						flag = false;
					}else{
						Date user_date = DateTimeUtils.formatString2Date(userMap.get("user_date").toString(), "yyyy-MM-dd");
						if(user_date.after(start) && user_date.before(end)){
							
						}else{
							flag = false;
						}
					}
					break;
				case "user_score":
					if(userMap.get("user_score").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer user_score = Integer.parseInt(userMap.get("user_score").toString());
					if(user_score >= low && user_score <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "self_num":
					if(userMap.get("self_num").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer self_num = Integer.parseInt(userMap.get("self_num").toString());
					if(self_num >= low && self_num <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "idcard_num":
					if(userMap.get("idcard_num").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer idcard_num = Integer.parseInt(userMap.get("idcard_num").toString());
					if(idcard_num >= low && idcard_num <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "task_num":
					if(userMap.get("task_num").equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer task_num = Integer.parseInt(userMap.get("task_num").toString());
					if(task_num >= low && task_num <= hig){
						
					}else{
						flag = false;
					}
					break;
				case "is_rank":
					if(!labelCondition.getConditionOne().equals(userMap.get("is_rank"))){
						flag = false;
					}if (userMap.get("is_rank").equals("0")) {
						flag = false;
					}
					break;
				case "space_bg_img":
					if(!labelCondition.getConditionOne().equals(userMap.get("space_bg_img"))){
						flag = false;
					}if (userMap.get("space_bg_img").equals("0")) {
						flag = false;
					}
					break;
				case "acc_code":
					if(!labelCondition.getConditionOne().equals(userMap.get("acc_code"))){
						flag = false;
					}if (userMap.get("acc_code").equals("0")) {
						flag = false;
					}
					break;
				case "reg_time":
					start = DateTimeUtils.formatString2Date(labelCondition.getConditionOne(), "yyyy-MM-dd");
					end = DateTimeUtils.formatString2Date(labelCondition.getConditionTwo(), "yyyy-MM-dd");
					if(userMap.get("reg_time") .equals("0")){
						flag = false;
					}else{
						Date reg_time = DateTimeUtils.formatString2Date(userMap.get("reg_time").toString(), "yyyy-MM-dd");
						if(reg_time.after(start) && reg_time.before(end)){
							
						}else{
							flag = false;
						}
					}
					break;
				case "active_time":
					start = DateTimeUtils.formatString2Date(labelCondition.getConditionOne(), "yyyy-MM-dd");
					end = DateTimeUtils.formatString2Date(labelCondition.getConditionTwo(), "yyyy-MM-dd");
					if(userMap.get("active_time") .equals("0")){
						flag = false;
					}else{
						Date active_time = DateTimeUtils.formatString2Date(userMap.get("active_time").toString(), "yyyy-MM-dd");
						if(active_time.after(start) && active_time.before(end)){
							
						}else{
							flag = false;
						}
					}
					break;
				case "task_cancle_cnt":
					if(userMap.get("task_cancle_cnt") .equals("0")){
						flag = false;
					}
					low = Integer.parseInt(labelCondition.getConditionOne().toString());
					hig = Integer.parseInt(labelCondition.getConditionTwo().toString());
					Integer task_cancle_cnt = Integer.parseInt(userMap.get("task_cancle_cnt").toString());
					if(task_cancle_cnt >= low && task_cancle_cnt <= hig){
						
					}else{
						flag = false;
					}
					break;
				default:
					break;
				}
				/*if(labelCondition.getConditionTwo() != null && labelCondition.getConditionTwo()==0){
					
					if((int)userMap.get(labelCondition.getLabelField())== labelCondition.getConditionOne().intValue()){
						result += 1;
					}
				}else{//否则条件一和条件二为大小
					if((int)userMap.get(labelCondition.getLabelField())>=labelCondition.getConditionOne() && (labelCondition.getConditionTwo() != null && (int)userMap.get(labelCondition.getLabelField())<=labelCondition.getConditionTwo())){
						result += 1;
					}
				}*/
			}
			if(flag == true){
				LabelUser userInfo = new LabelUser();
				userInfo.setUserId(userId);
				userInfo.setLabelId(labelInfo.getId());
				userInfo.setLabelName(labelInfo.getLabelName());
				labelUser.add(userInfo);
			}
			//满足所有条件获得标签
			//if(labelInfo.getType()==1 && result==labelConditionList.size()){
			
			//只要满足一个条件即可获取标签
			/*if(labelInfo.getType()==2 && result>0){
				LabelUser userInfo = new LabelUser();
				userInfo.setUserId(userId);
				userInfo.setLabelId(labelInfo.getId());
				userInfo.setLabelName(labelInfo.getLabelName());
				labelUser.add(userInfo);
			}*/
		}
		
		//更新用户标签表
		List<LabelUser> labelList = labelUserMapper.selectAllUser(userId);//获取用户已经拥有标签
		//插入标签
		for(LabelUser labelOne : labelUser){
			boolean isInsert = true;
			for(LabelUser labeMapTwo : labelList){
				if(labelOne.getLabelId().equals(labeMapTwo.getLabelId())){//如果有相同的标签则跳出
					isInsert = false;
					break;
				}
			}
			if(isInsert){//没有相同标签插入新的标签
				labelUserMapper.insertUserByUserId(labelOne);
			}
			
		}
		//删除标签
		for(LabelUser labeTwo : labelList){
			boolean isDelete = true;
			for(LabelUser labelOne : labelUser){
				if(labelOne.getLabelId().equals(labeTwo.getLabelId())){//如果有相同的标签则跳出
					isDelete = false;
					break;
				}
			}
			if(isDelete){//没有相同标签则删除标签
				labelUserMapper.deleteUserByUserId(labeTwo);
			}
		}
		
		
		return labelUser;
	}
	
	public List<LinkedHashMap<String,Object>> selectLabelInfoByUserInfoId(LabelUser labelUser){
		List<LinkedHashMap<String,Object>> selectLabelUserByUserInfoId=labelInfoMapper.selectLabelInfoByUserInfoId(labelUser);
		return selectLabelUserByUserInfoId;
	}
	@Transactional
	public Integer setLabelInfoBylabelState(String labelInfo,String userId){
		if(StringUtils.isBlank(labelInfo)){
			labelUserMapper.deleteAll(userId);
		}else{
			if(labelInfo.endsWith(",")){
				labelInfo = labelInfo.substring(0, labelInfo.length()-1);
			}
			labelUserMapper.deleteAll(userId);
			labelUserMapper.updateBatch(labelInfo.split(","));
		}
//		int labelNum=labelInfoMapper.setLabelInfoBylabelState(labelUser);
		return null;
	}
	
}
